<?php


namespace app\api\controller;

use app\api\model\Member;
use app\api\model\Memberlog;
use app\api\model\Memberrule as rule;
use think\Db;
use think\Exception;

//https://cosimo.qswh.net.cn/api/Memberrule/buyMember
//http://cosmo.com/api/Memberrule/buyMember
class Memberrule extends BaseApi {

    public function getMemberRule(){
        $rule = new rule();
        $ruleItem = $rule->find();
        if(!empty($ruleItem)){
            $ruleItem->describe = $this->replacePath($ruleItem->describe);
            return json(['code'=>1,'data'=>$ruleItem]);
        }
        return json(['code'=>-1,'msg'=>'暂无会员的规则']);
    }

    //我的团队
    public function myTeam(){
        if(!$this->request->param('openid')){
            return json(['code'=>-1,'msg'=>'缺少参数']);
        }

        $openid = trim($this->request->param('openid'));

        $member = new Member();
        $memberItem = $member->getMember($openid);
        if(empty($memberItem)){
            return json(['code'=>-1,'msg'=>'该用户不存在或已删除']);
        }

        //获取直属下级
//        $memberTeam = $this->infiniteMember($memberItem);
        $memberTeam = $member->getTeam($memberItem->id,['nickname','username','membersn','periodtime','avatar_image']);
        return json(['code'=>1,'data'=>$memberTeam]);
    }
    //无限分类获取下级
    protected function infiniteMember($memberItem){
        $member = new Member();
        $memberTeam = $member->getTeams(['id','nickname','username','membersn','periodtime','avatar_image','agentid','openid']);
        if(empty($memberTeam)){
            return json(['code'=>-1,'msg'=>'暂无下线团队']);
        }

        $memberTeam = $this->sort($memberTeam,$memberItem->id);
        return $memberTeam;
    }
    protected function sort($data,$pid=0){
        static $arr = array();
        foreach($data as $k=>$v){
            if($v['agentid'] == $pid){
                $arr[] = $v;
                $this->sort($data,$v['id']);
            }
        }
        return $arr;
    }


    //购买成为会员
    public function buyMember(){
        if(!$this->request->param('openid') || !$this->request->param('memberid')){
            return json(['code'=>-1,'msg'=>'缺少参数']);
        }

        $ruleid = intval($this->request->param('memberid'));
        $openid = trim($this->request->param('openid'));

        $rule = new rule();
        $ruleItem = $rule->field('id,money')->where('id',$ruleid)->find();

        if(empty($ruleItem)){
            return json(['code'=>-1,'msg'=>'规则不存在或已删除']);
        }

        Db::startTrans();
        try{
            $memberLog = new Memberlog();
            $logsn = $this->checkOrderUniqueness('ME','ME'.date('YmdHis').$this->random(6));
            $memberLog->openid = $openid;
            $memberLog->logsn = $logsn;
            $memberLog->price = $ruleItem->money;
            $memberLog->status = 0;
            $memberLog->createtime = time();
            $memberLog->save();

            $logid = $memberLog->id;
            if(!$logid){
                return json(['code'=>-1,'msg'=>'订单生成失败']);
            }

            //完成支付
            $payData = [
                'total_fee' => $ruleItem->money,
                'out_trade_no' => $logsn,
                'openid' => $openid
            ];
            $payResult = $this->processPay('member',$payData,'购买会员');
            if(empty($payResult)){
                return ['code'=>-1,'msg'=>'订单支付失败'];
            }
            Db::commit();
            return json(['code'=>1,'data'=>$payResult]);
        }catch (Exception $e){
            Db::rollback();
            return json(['code'=>-1,'msg'=>$e->getMessage()]);
        }





    }


    //验证订单编号的唯一性
    protected function checkOrderUniqueness($prefix,$sn){
        $order = new Memberlog();
        $orderItem = $order->where('logsn',$sn)->find();
        if(!empty($orderItem)){
            return $prefix.date('YmdHis').$this->random(6);
        }
        return $sn;
    }


}





